using B.S.BaseData.Infrastructure;
using B.S.BaseData.Infrastructure.Implements;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.WriteAPI.Audit;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
using Yitter.IdGenerator;
using NLog.Web;
using Microsoft.OpenApi.Models;
using ServiceManagement.Consul;
using B.S.BaseData.WriteAPI.Utils;
using B.S.BaseData.WriteAPI.Extensions;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers(options =>
{
    // 全局注册操作日志过滤器
    //options.Filters.Add<LogOperationAttribute>();
});
// 注册HttpContextAccessor
builder.Services.AddHttpContextAccessor();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(d =>
{
    string xml = AppContext.BaseDirectory + "B.S.BaseData.WriteAPI.xml";
    d.IncludeXmlComments(xml, true);

    //定义一个Swagger文档 Users组
    d.SwaggerDoc("Users", new OpenApiInfo { Title = "Users API", Version = "用户管理" });
    //定义第二个Swagger文档 Roles组
    d.SwaggerDoc("Roles", new OpenApiInfo { Title = "Roles API", Version = "角色管理" }); 
    d.SwaggerDoc("Depts", new OpenApiInfo { Title = "Depts API", Version = "部门管理" });
    d.SwaggerDoc("Perms", new OpenApiInfo { Title = "Perms API", Version = "权限管理" });
    d.SwaggerDoc("MeetingRoom", new OpenApiInfo { Title = "MeetingRoom API", Version = "会议室管理" });
    d.SwaggerDoc("MeetingReservation", new OpenApiInfo { Title = "MeetingReservation API", Version = "会议室预约管理" });
    d.SwaggerDoc("MeetingApproval", new OpenApiInfo { Title = "MeetingApproval API", Version = "会议室审批管理" });
    d.SwaggerDoc("OperationLog", new OpenApiInfo { Title = "OperationLog API", Version = "日志管理" });

    //配置文档包含的规则
    //如果没有指定GroupName，则默认使用ControllerName进行分组
    //DocInclusionPredicate用于控制那些api描述应该包含在哪个swagger文档中
    d.DocInclusionPredicate((docName, apiDesc) =>
    {
        var groupName = apiDesc.GroupName ?? apiDesc.ActionDescriptor.RouteValues["controller"];
        //如果文档名称和groupname相同，则包含该api
        return docName == groupName;
    });
});

//用法1-CSRedisClient实例化的对象
var rds = new CSRedis.CSRedisClient(builder.Configuration.GetConnectionString("RedisStr"));
builder.Services.AddSingleton(rds);   //注册成全局单例

//转换大小写
builder.Services.AddControllers().AddJsonOptions(d => d.JsonSerializerOptions.PropertyNamingPolicy = null);

//配置数据库
builder.Services.AddDbContext<BaseDbContext>(d =>
{
    d.UseSqlServer(builder.Configuration.GetConnectionString("con"));
});

// 添加审计拦截器服务
builder.Services.AddScoped<AuditInterceptor>();

// 配置DbContext选项
builder.Services.AddScoped<DbContextOptions>(provider => 
{
    var optionsBuilder = new DbContextOptionsBuilder<BaseDbContext>();
    optionsBuilder.UseSqlServer(builder.Configuration.GetConnectionString("con"));
    
    // 使用依赖注入获取拦截器
    var interceptor = provider.GetRequiredService<AuditInterceptor>();
    optionsBuilder.AddInterceptors(interceptor);
    
    return optionsBuilder.Options;
});

//注册服务
builder.Services.AddServices();
//注册jtw
builder.Services.AddJwt(builder);
//日志
builder.Host.UseNLog();

//注册consul
builder.Configuration.Register();

// 注册自定义服务
builder.Services.AddCustomServices();



    




var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(x =>
    {
        //为每个分组配置不同的Swagger文档
        x.SwaggerEndpoint("/swagger/Users/swagger.json", "Users API 用户管理");
        x.SwaggerEndpoint("/swagger/Roles/swagger.json", "Roles API 角色管理");
        x.SwaggerEndpoint("/swagger/Depts/swagger.json", "Depts API 部门管理");
        x.SwaggerEndpoint("/swagger/Perms/swagger.json", "Perms API 权限管理");
        x.SwaggerEndpoint("/swagger/MeetingRoom/swagger.json", "MeetingRoom API 会议室管理");
        x.SwaggerEndpoint("/swagger/MeetingReservation/swagger.json", "MeetingReservation API 会议室预约管理");
        x.SwaggerEndpoint("/swagger/MeetingApproval/swagger.json", "MeetingApproval API 会议室审批管理");
        x.SwaggerEndpoint("/swagger/OperationLog/swagger.json", "OperationLog API 日志管理");

    });
}



//跨域
app.UseCors(d => d.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());

//设置静态文件
app.UseStaticFiles();

//雪花id
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());

app.UseRouting();

// 认证中间件必须在授权之前
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();
